/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.itip.util;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;

/**
 * <p>
 * Description: DB2数据库连接类
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-28 上午9:35:42          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class DB2ConnectionUtil {

    private static final Logger logger = Logger.getLogger(DB2ConnectionUtil.class);

    private static DB2ConnectionUtil instance = null;

    private static Object _lock = new Object();

    private BasicDataSource ds = null;
    private BasicDataSource mysqlDS = null;

    private DB2ConnectionUtil() {

    }

    /**
     * 保证DB2连接池只有一个
     * @return
     */
    public static synchronized DB2ConnectionUtil getInstance() {
        if (instance == null) {
            synchronized (_lock) {
                if (instance == null) {
                    instance = new DB2ConnectionUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化数据库连接池
     */
    public void initDBPool(String tivoliDriver, String url, String user, String pwd) {
        ds = new BasicDataSource();
        ds.setDriverClassName(tivoliDriver);
        ds.setUsername(user);
        ds.setPassword(pwd);
        ds.setUrl(url);
        ds.setMaxActive(1);
        ds.setMaxIdle(1);
        ds.setMinIdle(0);
        ds.setMaxWait(5 * 60 * 1000L);
        ds.setTestOnBorrow(true);
        logger.info("Create new db2 connection ");
    }

    public void initMysqlPool(String mysqlDriver, String url, String user, String pwd) {
        mysqlDS = new BasicDataSource();
        mysqlDS.setDriverClassName(mysqlDriver);
        mysqlDS.setUsername(user);
        mysqlDS.setPassword(pwd);
        mysqlDS.setUrl(url);
        mysqlDS.setMaxActive(1);
        mysqlDS.setMaxIdle(1);
        mysqlDS.setMinIdle(0);
        mysqlDS.setMaxWait(5 * 60 * 1000L);
        mysqlDS.setTestOnBorrow(true);
        logger.info("Create new mysql connection ");
    }

    /**
     * 获取一个连接  mysql
     * @return
     */
    public Connection getMysqlConnection() {
        Connection conn = null;
        try {
            conn = mysqlDS.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    /**
     * 
     * @return
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = ds.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    /**
     * dbcp使用委派模型来实现Connection接口
     * 故.close()就成连接归还到连接池 而不会关闭连接
     */
    public void returnConn(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            logger.error("Failed To Return Conn To DS", e);
        }
    }

}
